package com.mytijian.openapi.adapter.dianping.service.impl;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mytijian.openapi.adapter.dianping.consts.DzopenConsts;
import com.mytijian.openapi.adapter.dianping.exceptions.DzopenErrorInfo;
import com.mytijian.openapi.adapter.dianping.exceptions.DzopenException;
import com.mytijian.openapi.adapter.dianping.model.DzopenOrderRel;
import com.mytijian.openapi.adapter.dianping.param.DzCancelbookParam;
import com.mytijian.openapi.adapter.dianping.param.DzopenOrderRelQuery;
import com.mytijian.openapi.adapter.dianping.result.DzCancelbookResult;
import com.mytijian.openapi.adapter.dianping.service.DzopenCancelbookService;
import com.mytijian.openapi.adapter.dianping.service.DzopenOrderRelService;
import com.mytijian.openapi.core.client.IOpenAPIClient;
import com.mytijian.openapi.core.client.result.BaseResult;
import com.mytijian.openapi.core.model.AppConfig;
import com.mytijian.openapi.core.service.OpenApiAppConfigService;

@Service("dzopenCancelbookService")
public class DzopenCancelbookServiceImpl implements DzopenCancelbookService {

	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private DzopenOrderRelService dzopenOrderRelService;

	@Autowired
	private OpenApiAppConfigService openApiAppConfigService;

	@Autowired
	private IOpenAPIClient client;

	@Override
	public DzCancelbookResult cancelbook(DzCancelbookParam param) {
		
		logger.info("[美团点评][撤单接口] 入参{}", ToStringBuilder.reflectionToString(param));
		
		AppConfig config = openApiAppConfigService.getConfig(DzopenConsts.AppName);

		DzopenOrderRel dzOrderRel = getOrderRel(param);
		
		checkOrderStatus(dzOrderRel);
		
		BaseResult revokeResult = client.revoke(config, dzOrderRel.getOrderNum());

		logger.info("[美团点评][撤单接口] openAPI返回{}",ToStringBuilder.reflectionToString(revokeResult));

		verifyRevokeResult(revokeResult);
		
		dzOrderRel.setOrderConsumeStatus(DzopenConsts.OrderConsumeStatus.canceled);
		dzopenOrderRelService.updateDzopenOrderRel(dzOrderRel);
	
		DzCancelbookResult result = new DzCancelbookResult();
		result.setOrder_id(param.getOrderId());
		
		return result;
	}

	private DzopenOrderRel getOrderRel(DzCancelbookParam param) {
		DzopenOrderRelQuery query = new DzopenOrderRelQuery();
		query.setDzOrderId(param.getOrderId());
		DzopenOrderRel dzOrderRel = dzopenOrderRelService.getOrderRel(query);
		return dzOrderRel;
	}
	
	private void verifyRevokeResult(BaseResult revokeResult) {
		if (revokeResult==null || !"T".equals(revokeResult.getSuccess())) {
			throw new DzopenException(DzopenErrorInfo.ORDER_CANCEL_UNKNOW);
		}
	}

	private void checkOrderStatus(DzopenOrderRel dzOrderRel) {
		if (dzOrderRel==null) {
			throw new DzopenException(DzopenErrorInfo.ORDER_CANCEL_NOT_FOUND);
		}
		if (DzopenConsts.OrderConsumeStatus.canceled == dzOrderRel.getOrderConsumeStatus()) {
			throw new DzopenException(DzopenErrorInfo.ORDER_CANCEL_CONSUMED);
		}
		
		if (DzopenConsts.OrderConsumeStatus.consumed == dzOrderRel.getOrderConsumeStatus()) {
			throw new DzopenException(DzopenErrorInfo.ORDER_CANCEL_CONSUMED);
		}
	}
}
